#!/bin/bash
sandboxdir=$(dirname $0)
. $sandboxdir/sb_vars.sh
. $sandboxdir/sb_common.sh


if [ $HOW_MANY_NODES -lt 2 ]
then
    grep HOW_MANY_NODES $sandboxdir/sb_vars.sh
    echo "Can't create a multiple sandbox with less than 2 nodes"
    exit 1
fi

SANDBOX_BINARY=$HOME/opt/mysql
TARGET=$1
if [ -n "$TARGET" ] 
then
    if [ -d $SANDBOX_BINARY/$TARGET ]
    then
        echo "# $TARGET found in $SANDBOX_BINARY"
        if [ "$TARGET" != "$MYSQL_VERSION" ]
        then
            echo "The MySQL version defined in sb_vars.sh ($MYSQL_VERSION) does not match the version provided here ($TARGET)"
            echo "If you want to create tungsten sandboxes with dynamic variables, use '$sandboxdir/tungsten-sandbox -m $TARGET' "
            exit 1
        fi
    elif [ -f $TARGET ]
    then
        tarball_version=$(perl -le '$T=shift;print "$1" if $T =~ /(?:mysql|mariadb|percona).*(\d+\.\d+\.\d+).*\.tar\.gz/i ' $TARGET )
        if [ -z "$tarball_version" ]
        then
            echo "unable to find a version number in '$TARGET'"
            exit 1
        fi
        if [ "$tarball_version"  != "$MYSQL_VERSION" ]
        then
            echo "The MySQL version defined in sb_vars.sh ($MYSQL_VERSION) does not match the version from the tarball ($tarball_version)"
            exit 1
        fi
        if [ ! -d $SANDBOX_BINARY ]
        then
            mkdir -p $SANDBOX_BINARY
        fi
        if [ -d $SANDBOX_BINARY/$tarball_version ]
        then
            echo "There is already a directory $tarball_version in $SANDBOX_BINARY"
            echo "$TARGET will not be expanded"
            exit 1
        fi
        tar -xzf $TARGET
        if [ "$?" != "0" ]
        then
            echo "error extracting from $TARGET"
            exit 1
        fi
        TARBALL_DIR=$(basename $TARGET .tar.gz)
        if [ ! -d $TARBALL_DIR ]
        then
            echo "error extracting $TARBALL_DIR from $TARGET"
            exit 1
        fi
        mv $TARBALL_DIR $SANDBOX_BINARY/$tarball_version
    else
        echo "'$TARGET' is neither a directory in $SANDBOX_BINARY or a tarball file"
        exit 1
    fi
fi    

if [ ! -d $SANDBOX_BINARY/$MYSQL_VERSION ]
then
    echo "MySQL version $MYSQL_VERSION not found in $SANDBOX_BINARY"
    exit 1
fi

if [ -d $HOME/sandboxes/$SB_DIRECTORY ]
then
    sbtool -o delete -s $HOME/sandboxes/$SB_DIRECTORY
fi
NODE_OPTIONS="-c innodb-thread-concurrency=0"
NODE_OPTIONS="$NODE_OPTIONS -c sync_binlog=1"
NODE_OPTIONS="$NODE_OPTIONS -c skip-name-resolve"
NODE_OPTIONS="$NODE_OPTIONS -c innodb-log-buffer-size=50M"
NODE_OPTIONS="$NODE_OPTIONS -c max-connections=150"
NODE_OPTIONS="$NODE_OPTIONS -c max_allowed_packet=52M"
NODE_OPTIONS="$NODE_OPTIONS -c innodb_buffer_pool_size=64M"
NODE_OPTIONS="$NODE_OPTIONS -c innodb-log-file-size=50M"
NODE_OPTIONS="$NODE_OPTIONS -c default_storage_engine=innodb"
NODE_OPTIONS="$NODE_OPTIONS -c innodb-flush-method=O_DIRECT"
NODE_OPTIONS="$NODE_OPTIONS --remote_access=%"

if [ -n "$BINLOG_FORMAT" ]
then
    if [ -n "$VERBOSE" ]
    then
        echo "# Setting binlog_format to $BINLOG_FORMAT"
    fi
    NODE_OPTIONS="$NODE_OPTIONS -c binlog-format=$BINLOG_FORMAT"
fi
if [ -n "$MULTIPLE_TIMEZONE" ]
then
    MORE_TZ_OPTIONS=""
    NODE_NUM=1
    for TZ_NUM in $(seq 0 $(($HOW_MANY_NODES-1)))
    do
        MORE_TZ_OPTIONS="$MORE_TZ_OPTIONS --one_node_options='$NODE_NUM:-c default-time-zone=+0$TZ_NUM:00'"
        NODE_NUM=$(($NODE_NUM+1))
    done
else
    [ -z "$DBTIMEZONE" ] && DBTIMEZONE="+00:00"
    NODE_OPTIONS="$NODE_OPTIONS -c default-time-zone=$DBTIMEZONE"
fi

SB_COMMAND="make_multiple_sandbox --group_directory=$SB_DIRECTORY --how_many_nodes=$HOW_MANY_NODES --node_options='$NODE_OPTIONS' $MORE_TZ_OPTIONS --sandbox_base_port=$MYSQL_BASE_PORT $MYSQL_VERSION"

if [ -n "$DRYRUN" -o -n "$VERBOSE" ]
then
    [ -n "$MAKE_INI" ] && exit
    echo "# Building $HOW_MANY_NODES sandboxes with MySQL $MYSQL_VERSION"
    show_command "$SB_COMMAND"
    [ -n "$DRYRUN" ] && exit 
fi

sb_exec=$TUNGSTEN_SB/sb_cmd
echo $SB_COMMAND > $sb_exec
if [ ! -f $sb_exec ]
then
    echo "error  creating $sb_exec"
    exit 1
fi
chmod +x $sb_exec
$sb_exec

if [ -n "$USE_SSL" ]
then
    mkdir $HOME/sandboxes/$SB_DIRECTORY/ssl
    cp $sandboxdir/ssl/*.pem $HOME/sandboxes/$SB_DIRECTORY/ssl

    for N in $(seq 1 $HOW_MANY_NODES)
    do
        perl -i -pe 's/(\[client\])/$1\n$ENV{CLIENT_OPTIONS}\n/' $HOME/sandboxes/$SB_DIRECTORY/node$N/my.sandbox.cnf
        perl -i -pe 's/(\[mysqld\])/$1\n$ENV{SERVER_OPTIONS}\n/' $HOME/sandboxes/$SB_DIRECTORY/node$N/my.sandbox.cnf  
        $HOME/sandboxes/$SB_DIRECTORY/node$N/restart
    done
fi

for N in $(seq 1 $HOW_MANY_NODES)
do
    echo "# Node $N - Creating replication user '$MYSQL_USER' "
    SSL_OPTIONS=''
    if [ -n "$USE_SSL" ]
    then
        SSL_OPTIONS='require ssl'    
    fi
    if [ -n "$VERBOSE" ]
    then
        echo $HOME/sandboxes/$SB_DIRECTORY/n$N -u root -e "grant all on *.* to $MYSQL_USER identified by '$MYSQL_PASSWORD' $SSL_OPTIONS with grant option"
    fi
    $HOME/sandboxes/$SB_DIRECTORY/n$N -u root -e "grant all on *.* to $MYSQL_USER identified by '$MYSQL_PASSWORD' $SSL_OPTIONS with grant option"
    if [ -n "$UNPRIVILEGED_USERS" ]
    then
        $HOME/sandboxes/$SB_DIRECTORY/n$N -u root -e "grant all on *.* to ${MYSQL_USER}_lite identified by '$MYSQL_PASSWORD' $SSL_OPTIONS "
        $HOME/sandboxes/$SB_DIRECTORY/n$N -u root -e "revoke SUPER on *.* from ${MYSQL_USER}_lite "
    fi

    #if [ -n "$BINLOG_FORMAT" ]
    #then
    #    $HOME/sandboxes/$SB_DIRECTORY/n$N -u root -e "set global binlog_format=$BINLOG_FORMAT"
    #fi
done

